//定义需要的元素的全局变量，因为这些变量经常需要进行判断，判断成功后要进行取值，所以定义为全局变量是一种不错的方式。
//当然要及时更新全局变量的值，且要及时取值。尽最大可能做到：取的值是刚判断过的。
var email, phone, inputCode, pwd, retryPwd;
//邮箱正则表达式
var emailReg = /^[\w_\.]+@[\w-]+(\.[\w-]+)*\.[0-9a-zA-Z0]{2,6}$/;
//手机正则表达式
var phoneReg = /^1[3|4|5|6|7|8]\d{9}$/;
//判断邮箱是否为空和格式正确与否
function validateEmail() {
    email = $("#email").val();
    if (email == null || email.length == 0) {
        alert('温馨小提示:\n邮箱不能为空！');
        return false;
    }
    if (!emailReg.test(email)) {
        alert('温馨小提示：\n请输入有效的邮箱格式！');
        emailReg.focus();
        return false;
    }
    console.log('邮箱验证通过！');
    return true;
}

function validatePhone() {
    phone = $("#phone").val();
    if (phone == null || phone.length == 0) {
        alert('温馨小提示:\n手机号不能为空！');
        return false;
    }
    if (!phoneReg.test(phone)) {
        alert('温馨小提示：\n请输入有效的手机号格式！');
        phoneReg.focus();
        return false;
    }
    console.log('手机号验证通过！');
    return true;
}

//判断验证码是否为空，如果为空则不传到后端
function validateCode(inputCodeId) {
    inputCode = inputCodeId.val();
    if (inputCode == null || inputCode.length == 0) {
        alert('请输入验证码-_-');
        return false;
    }
    return true;
}

//判断输入的两次密码格式是否正确，与两次密码是否一致
function validate2Pwd(id1, id2) {
    // var pwd = $("pwd1").val();
    pwd = id1.val();
    if (pwd == null || pwd.length == 0) {
        alert('密码不能为空！');
        return false;
    }
    if (pwd.length < 8) {
        alert('密码至少8位！');
        return false;
    }
    // var retryPwd = $("#retryPwd1").val();
    retryPwd = id2.val();
    if (pwd != retryPwd) {
        alert('输入的两次密码不一致！');
        return false;
    }
    return true;
}

//获取手机验证码
function getPhoneCode() {
    if (validatePhone()) {
        $.ajax({
            url: '/yuepai/user/getPhoneCode.action',
            type: 'post',
            data: {
                phone: phone,
                templateId: 2 // 短信：密码重置模板
            },
            dataType: 'json',
            success: function (res) {
                if (res.status == 200) {
                    alert('发送成功！ 注意查收*_*');
                }
            }
        });
        return true;
    }
}

//获取邮箱验证码
function getEmailCode() {
    if (validateEmail()) {
        $.ajax({
            url: '/yuepai/user/getEmailCode.action',
            type: 'post',
            data: {
                email: email,
                subject: '重置密码'
            },
            dataType: 'json',
            success: function (res) {
                if (res.status == 200) {
                    alert('发送成功！');
                }
            }
        });
        return true;
    }
}

//通过手机号重置密码
function resetPwdByPhone() {
    if (validatePhone()) {
        if (validateCode($("#input_phone_code"))) {
            if (validate2Pwd($("#pwd1"), $("#retryPwd1"))) {
                console.log('email:' + phone + "\ncode:" + inputCode + "\nnewPwd:" + pwd);
                $.ajax({
                    url: '/yuepai/user/resetPwd.action',
                    type: 'post',
                    data: {
                        num: phone,
                        code: inputCode,
                        newPwd: pwd,
                        resetType: '手机号'
                    },
                    dataType: 'json',
                    success: function (res) {
                        console.log('res:' + res);
                        // if (res.status == 200) {
                        //     document.getElementById("#countDownId").style.display = "block";
                        //     timer = setInterval("CountDown()", 1000);
                        // }
                        window.location.href = "/yuepai/user/login";
                    }
                });
            }
        }

    }

}



//通过邮箱重置密码
function resetPwdByEmail() {
    if (validateEmail()) {
        if (validateCode($("#input_email_code"))) {
            if (validate2Pwd($("#pwd2"), $("#retryPwd2"))) {
                console.log('email:' + email + "\ncode:" + inputCode + "\nnewPwd:" + pwd);
                $.ajax({
                    url: '/yuepai/user/resetPwd.action',
                    type: 'post',
                    data: {
                        num: email,
                        code: inputCode,
                        newPwd: pwd,
                        resetType: '邮箱'
                    },
                    dataType: 'json',
                    success: function (res) {
                        console.log('res:' + res.message);
                        if (res.status == 200) {
                            // document.getElementById("#countDownId").style.display = "block";
                            // timer = setInterval("CountDown()", 1000);
                            window.location.href = "/yuepai/user/login";
                        }
                    }
                });
            }
        }

    }

}




//---实现页面反转  start---
var x, y, ny = 0, nx = 270, rotYINT
function begin() {
    y = document.getElementById("rotatey1");
    x = document.getElementById("rotatey2");
    clearInterval(rotYINT);
    rotYINT = setInterval("startYRotate11()", 5);
    setTimeout("setValue()", 600);

}
function end() {
    clearInterval(rotYINT);
    rotYINT = setInterval("startYRotate21()", 5);
    setTimeout("setValue1()", 600);
}
function setValue() {
    y.style.display = "none";
    x.style.display = "block";
    clearInterval(rotYINT);
    rotYINT = setInterval("startYRotate12()", 5);
}
function setValue1() {
    x.style.display = "none";
    y.style.display = "block";
    clearInterval(rotYINT);
    rotYINT = setInterval("startYRotate22()", 5);
}
//从270到360 1
function startYRotate12() {
    nx = nx + 1;
    x.style.transform = "rotateY(" + nx + "deg)";
    x.style.webkitTransform = "rotateY(" + nx + "deg)";
    x.style.OTransform = "rotateY(" + nx + "deg)";
    x.style.MozTransform = "rotateY(" + nx + "deg)";
    if (nx >= 360) {
        nx = 270;
        clearInterval(rotYINT);
    }
}
//从270到360 2
function startYRotate22() {
    nx = nx + 1;
    y.style.transform = "rotateY(" + nx + "deg)";
    y.style.webkitTransform = "rotateY(" + nx + "deg)";
    y.style.OTransform = "rotateY(" + nx + "deg)";
    y.style.MozTransform = "rotateY(" + nx + "deg)";
    if (nx >= 360) {
        nx = 270;
        clearInterval(rotYINT);
    }
}
//从0-90 把1
function startYRotate11() {
    ny = ny + 1;
    y.style.transform = "rotateY(" + ny + "deg)";
    y.style.webkitTransform = "rotateY(" + ny + "deg)";
    y.style.OTransform = "rotateY(" + ny + "deg)";
    y.style.MozTransform = "rotateY(" + ny + "deg)";
    if (ny >= 90) {
        ny = 0;
        clearInterval(rotYINT);
    }
}
// 0-90 把2
function startYRotate21() {
    ny = ny + 1;
    x.style.transform = "rotateY(" + ny + "deg)";
    x.style.webkitTransform = "rotateY(" + ny + "deg)";
    x.style.OTransform = "rotateY(" + ny + "deg)";
    x.style.MozTransform = "rotateY(" + ny + "deg)";
    if (ny >= 90) {
        ny = 0;
        clearInterval(rotYINT);
    }
}

//---实现页面反转  end---


//---实现获取验证码按钮被点击后出现倒计时功能，且按钮在此期间不可用  start---
$(function () {
    //加载刷新事件
    load($("#phoneCode"));
    load($("#emailCode"));
    //点击事件 如果手机号/邮箱验证通过则进入倒计时
    $("#phoneCode").click(function () {
        if (getPhoneCode()) {
            time($("#phoneCode"));
            // time(this);
        }
    });
    $("#emailCode").click(function () {
        if (getEmailCode()) {
            time($("#emailCode"));
            // time(this);
        }

    });
})
function time(obj) {
    var wait = 60;
    setLocalDelay(wait);
    if (wait == 0) {
        obj.attr("disabled", false);
        obj.html("获取验证码")
        setLocalDelay(wait);
    } else {
        var timer = setInterval(function () {
            if (wait > 0) {
                wait--;
                obj.attr("disabled", true);
                obj.html("剩余(" + wait + ")秒")
                setLocalDelay(wait);
            } else {
                clearInterval(timer);
                obj.attr("disabled", false);
                obj.html("获取验证码")
            }
        }, 1000);
    }
}
function load(obj) {
    //获取缓存中的值
    var timers = getLocalDelay();
    if (timers.lastname == null || timers.lastname == 0) {
        obj.html("获取验证码")
        return;
    }
    obj.html("剩余(" + timers.lastname + ")秒")
    var timer = setInterval(function () {
        if (timers.lastname > 0) {
            timers.lastname--;
            obj.attr("disabled", true);
            obj.html("剩余(" + timers.lastname + ")秒")
            setLocalDelay(timers.lastname);
        } else {
            clearInterval(timer);
            obj.attr("disabled", false);
            obj.html("获取验证码")
        }
    }, 1000);
}
function setLocalDelay(time) {
    //location.href作为页面的唯一标识，可能一个项目中会有很多页面需要获取验证码。
    localStorage.setItem("lastname" + location.href, time);
}
function getLocalDelay() {
    var LocalDelay = {};
    LocalDelay.lastname = localStorage.getItem("lastname" + location.href);
    return LocalDelay;
}
//---按钮倒计时  end ---

//错误的回调函数
function errorFun(res) {
    console.log(res.message + '\n遇到bug啦！   紧急呼叫程序猿geigei!');
}

//5秒倒计时
var maxtime = 5;
function CountDown() {
    if (maxtime >= 0) {
        var second = Math.floor(maxtime % 6);
        var msg = second + "秒后自动跳转到登录界面";
        document.getElementById("#countDownId").innerHTML = msg;
        --maxtime;
    } else {
        clearInterval(timer);
        //自动跳转到登录界面
        window.location.href = "/yuepai/user/login";
    }
}

//页面背景动画效果
"use strict";
var html = document.documentElement;
var deg = 0;
var tick = function tick() {
    html.style.background = "background linear-gradient(" + deg++ +"deg, #e484be, #FE5457)";
    requestAnimationFrame(tick);
};
requestAnimationFrame(tick);